package com.yfz.day11;

import java.util.Arrays;

/**
 * 16. 最接近的三数之和
 */
class Solution {
    public int threeSumClosest(int[] nums, int target) {
        int diff = Integer.MAX_VALUE;
        int allSum = 0;
        Arrays.sort(nums);
        int len = nums.length;
        for(int i = 0 ;i <= len - 2;i++)
        {
            int x = nums[i];
            int sum = target - nums[i];
            int j = i+1,k = len -1;
            while(i<j){
                int abs = Math.abs((sum - nums[j] - nums[k]));
                System.out.println(abs);
                if(abs<diff){
                    allSum = nums[i] + nums[j] + nums[k];
                    diff = abs;
                }
            }
        }
        return allSum;
    }
}